﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

public partial class Lab_Default : System.Web.UI.Page
{
    private SqlConnection con;
    private int Labavid = 0;
    private bool afsluiten = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (ZoekPatient1.patid != 0)
        {
            con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con.Open();
            SqlCommand hTests = new SqlCommand(
                    "SELECT ta.TAID, lt.Naam, ta.uitslag, la.LABAVID FROM Labaanvraag AS la " +
                        "INNER JOIN Testaanvraag AS ta ON " +
                            "ta.labavid = la.LABAVID AND " +
                            "la.patient = @Patid AND " +
                            "la.status  = 0 " +
                        "INNER JOIN Labtest AS lt ON " +
                            "lt.LABID = ta.labid", con);
            SqlParameter Patid = new SqlParameter();
            Patid.ParameterName = "@Patid";
            hTests.Parameters.Add(Patid);
            hTests.Parameters["@Patid"].Value = ZoekPatient1.patid;
            SqlDataReader rTests = hTests.ExecuteReader();
            while (rTests.Read())
            {
                Lab_TestWaarde t = (Lab_TestWaarde)Page.LoadControl("~/Lab/TestWaarde.ascx");
                t.Taid = rTests.GetInt32(0);
                t.Naam = rTests.GetString(1);
                if (!rTests.IsDBNull(2))
                    t.Uitslag = rTests.GetString(2);
                TestWaardes.Controls.Add(t);
                if (Labavid == 0)
                    Labavid = rTests.GetInt32(3);
            }
            rTests.Close();
            Update.Visible = true;
            Sluit.Visible = true;
        }
    }

    protected void Sluit_Click(object sender, EventArgs e)
    {
        afsluiten = true;
        Update_Click(sender, e);
        SqlCommand hSluit = new SqlCommand(
            "UPDATE Labaanvraag SET status = 1 WHERE LABAVID = @Labavid", con);
        SqlParameter spLabavid = new SqlParameter();
        spLabavid.ParameterName = "@Labavid";
        hSluit.Parameters.Add(spLabavid);
        hSluit.Parameters["@Labavid"].Value = Labavid;
        hSluit.ExecuteNonQuery();
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        SqlCommand hUpdate = new SqlCommand(
            "UPDATE Testaanvraag SET uitslag = @Uitslag WHERE TAID = @Taid", con);
        SqlParameter spUitslag = new SqlParameter();
        SqlParameter spTaid = new SqlParameter();
        spUitslag.ParameterName = "@Uitslag";
        spTaid.ParameterName = "@Taid";
        hUpdate.Parameters.Add(spUitslag);
        hUpdate.Parameters.Add(spTaid);
        IEnumerator it = TestWaardes.Controls.GetEnumerator();
        while (it.MoveNext())
        {
            Object o = it.Current;
            if (!(o is Lab_TestWaarde))
                continue;
            Lab_TestWaarde t = (Lab_TestWaarde)it.Current;
            hUpdate.Parameters["@Uitslag"].Value = t.Uitslag;
            hUpdate.Parameters["@Taid"].Value = t.Taid;
            hUpdate.ExecuteNonQuery();
            if (afsluiten)
                t.Enabled = false;
        }
    }
}